package cn.fl.institution.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.institution.api.IPrjInsRedmInterfaceInfoApiService;
import cn.fl.institution.condition.PrjInsRedmInterfaceInfoQC;
import cn.fl.institution.constant.PrjInsRedmInfoConstant;
import cn.fl.institution.service.IPrjInsRedmInterfaceInfoService;
import cn.fl.institution.transform.PrjInsRedmInterfaceInfoVOTransform;
import cn.fl.institution.vo.PrjInsRedmInterfaceInfoVO;
import cn.fl.system.api.ISysInsCfgInfoApiService;
import cn.fl.system.condition.SysInsCfgInfoQueryCondition;
import cn.fl.system.dto.SysInsCfgInfoDTO;
import com.alibaba.fastjson.JSON;
import fl.ref.fribaseinfo.constant.FriBaseInfoConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 资管项目赎回表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjInsRedmInterfaceInfoServiceImpl implements IPrjInsRedmInterfaceInfoService {


    @Autowired
    private IPrjInsRedmInterfaceInfoApiService prjInsRedmInterfaceInfoApiService;

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Autowired
    private ISysInsCfgInfoApiService sysInsCfgInfoApiService;

    /**
     * 查询资方赎回接口信息列表
     * @param qc
     * @return
     * @throws BaseException
     */
    @Override
    public List<PrjInsRedmInterfaceInfoVO> selectInsRedmInterfaceInfoList(PrjInsRedmInterfaceInfoQC qc) throws BaseException {
        if (null == qc || StringUtils.isEmpty(qc.getPrjId())) {
            BaseLogUtils.newLogger("查询资方赎回接口信息列表失败,参数不完整!").setKeys("selectInsRedmInterfaceInfoList","qc="+JSON.toJSONString(qc),CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException("查询资方赎回接口信息列表失败,参数不完整.");
        }
        return PrjInsRedmInterfaceInfoVOTransform.toVOList(this.prjInsRedmInterfaceInfoApiService.selectInsRedmInterfaceInfoList(qc));
    }

    /**
     * 添加资方接口信息
     * @param entity
     * @return
     * @throws BaseException
     */
    @Override
    public Long addPrjInsRedmInterfaceInfoVO(PrjInsRedmInterfaceInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newLogger("新增对象不存在!").setKeys("add",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增对象不存在!");
        }
        return this.prjInsRedmInterfaceInfoApiService.add(PrjInsRedmInterfaceInfoVOTransform.toDTO(entity));
    }

    /**
     * 修改资方接口信息
     * @param entity
     * @return
     * @throws BaseException
     */
    @Override
    public boolean modifyPrjInsRedmInterfaceInfoVO(PrjInsRedmInterfaceInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newLogger("新增对象不存在!").setKeys("add",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增对象不存在!");
        }
        return this.prjInsRedmInterfaceInfoApiService.modify(PrjInsRedmInterfaceInfoVOTransform.toDTO(entity));
    }


    /**
     * 获取资管接口信息机构配置 是否展示接口信息
     * @param insCode
     * @return
     */
    @Override
    public int getPrjInsRedmInterfaceConfig(String insCode) {
        return prjInsRedmInterfaceInfoApiService.getPrjInsRedmInterfaceConfig(insCode);
    }

    @Override
    public PrjInsRedmInterfaceInfoVO selectById(Long id) throws BaseException {
        return PrjInsRedmInterfaceInfoVOTransform.toVO(prjInsRedmInterfaceInfoApiService.selectById(id));
    }

    /**
     * 试算接口 SKD004
     *
     * @param qc
     */
    @Override
    public DataResultDTO calculation(PrjInsRedmInterfaceInfoQC qc) {
        return prjInsRedmInterfaceInfoApiService.calculation(qc);
    }


    /**
     * 贷款代偿 SKD006
     * @param qc
     */
    @Override
    public DataResultDTO applyFundClear(PrjInsRedmInterfaceInfoQC qc) {
        return prjInsRedmInterfaceInfoApiService.applyFundClear(qc);
    }


    /**
     * 订单取回 SKD007
     * @param qc
     */
    @Override
    public DataResultDTO cancelOrder(PrjInsRedmInterfaceInfoQC qc) {
        return prjInsRedmInterfaceInfoApiService.cancelOrder(qc);
    }

    /**
     * 获取机构回购主体
     * @param insCode
     * @return
     */
    @Override
    public List<KeyValueVO> getSuretySubjectCodeList(String insCode) {
        List<KeyValueVO> suretySubjectCodeList = this.sysDictionaryApiService.getKvList(FriBaseInfoConstant.FUND_REPORT_ZT, WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD);
        BaseLogUtils.newLogger("查询回购主体列表!").setKeys("getSuretySubjectCodeList","insCode=" + insCode,"suretySubjectCodeList="+ JSON.toJSONString(suretySubjectCodeList)).info();
        Map<String, KeyValueVO> suretySubjectCodeMap = new HashMap<>(suretySubjectCodeList.size());
        for (KeyValueVO keyValueVO : suretySubjectCodeList) {
            suretySubjectCodeMap.put(keyValueVO.getKey(), keyValueVO);
        }
        //查询机构回购主体配置
        SysInsCfgInfoQueryCondition sysInsCfgInfoQueryCondition = new SysInsCfgInfoQueryCondition();
        //配置代码
        sysInsCfgInfoQueryCondition.setCfgCd(PrjInsRedmInfoConstant.SYS_INS_CFG_INFO_R001);
        sysInsCfgInfoQueryCondition.setInsCode(insCode);
        List<SysInsCfgInfoDTO> sysInsCfgInfoDTOList = this.sysInsCfgInfoApiService.searchSysInsCfgInfoList(sysInsCfgInfoQueryCondition);
        if (CollectionUtils.isNotEmpty(sysInsCfgInfoDTOList)) {
            BaseLogUtils.newLogger("查询机构回购主体配置列表开始!").setKeys("getSuretySubjectCodeList","insCode=" + insCode,"stringSysInsCfgInfoDTOMap="+ JSON.toJSONString(sysInsCfgInfoDTOList)).info();
            String insCfgVal = sysInsCfgInfoDTOList.get(0).getCfgVal();
            BaseLogUtils.newLogger("查询机构回购主体配置列表结束!").setKeys("getSuretySubjectCodeList","insCode=" + insCode,"insCfgVal="+ insCfgVal).info();
            if (StringUtils.isNotBlank(insCfgVal)) {
                suretySubjectCodeList.clear();
                String[] insCfgValArray = insCfgVal.split("|");
                for (int i = 0; i < insCfgValArray.length; i++) {
                    if (null != suretySubjectCodeMap.get(insCfgValArray[i])) {
                        suretySubjectCodeList.add(suretySubjectCodeMap.get(insCfgValArray[i]));
                    }
                }
            }

        }
        return suretySubjectCodeList;
    }

    /**
     * 获取资管机构赎回配置
     * @param insCode 机构CODE
     * @param cfgCd 配置code
     * @return
     */
    @Override
    public int getPrjInsRedmConfig(String insCode, String cfgCd) {
        return prjInsRedmInterfaceInfoApiService.getPrjInsRedmConfig(insCode,cfgCd);
    }


}
